<?php

class Profile extends MY_Controller
{

   function __construct()
   {
		parent::__construct();
		
		if(!$this->admin) redirect('admin990/login');
   
   }
   
   
   function index()
   {
   	echo '';
      $data['title'] = 'Administration';
      $data['user'] = admin::find('all');
       $this->load->view('admin/header',$data);
      $this->load->view('admin/profile',$data);
     
   }
   
   
   function edit()
   {	
   		$current_id = $this->uri->segment(4);
   	  	$data['user'] = admin::find_by_id($current_id);
   	  	$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]');
	    $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
   	  	if ($this->form_validation->run() == FALSE)
		{
			
		}
		else
		{	
			$new_password = $this->input->post('password');
			$id =  $this->input->post('id');
			$user = admin::find_by_id($id);
			$user->hashed_password = $this->hash_password($new_password);
		    $user->save();
			redirect('admin990/profile');
		}

   	  	$data['title'] = 'PROFILE';
   	  	$this->load->view('admin/header',$data);
   	  	$this->load->view('admin/edit_user',$data);
   	  	
   
   }
   


   
   function add_user()
   {	
   	
   	if($_POST){
   	  $user = admin::find('all');
   	  $this->form_validation->set_rules('username', "Mom d'utilisateur", 'trim|required|min_length[5]|max_length[12]|callback_check_username');
   	  $this->form_validation->set_rules('lastname', 'Nom', 'trim|required');
   	  $this->form_validation->set_rules('firstname', 'Prénom', 'trim|required');
	  $this->form_validation->set_rules('password', 'Mot de passe', 'trim|required|matches[passconf]');
	  $this->form_validation->set_rules('passconf', 'Confirmation mot de passe', 'trim|required');
	  $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_check_email');
	  	  if ($this->form_validation->run() == FALSE)
		{
			$data['msg_error'] = $this->form_validation->set_message('rule', 'Error Message');	
		$data['title'] = 'Nouveau administrateur';
    	$this->load->view('admin/header',$data);
      	$this->load->view('admin/add_user',$data);
		}
		else
		{	
			$password = $_POST['password'];
			$password = $this->hash_password($password);
			$user = admin::create (array(
							'username' =>$this->input->post('username'),
							'email'=>$this->input->post('email'),
							'hashed_password' =>$password,
							'firstname'=>$this->input->post('firstname'),
							'lastname'=>$this->input->post('lastname'),
							));
			redirect('admin990/profile');
		}
   	
   	}else{
   	  $data['title'] = 'Nouveau administrateur';
      $this->load->view('admin/header',$data);
      $this->load->view('admin/add_user',$data);
   	
   	}		  
   }
   
   public function check_username()
   {
   		$username =  $this->input->post('username');
   		$user = admin::find_by_username($username);
   		
   		if($user){
   			$this->form_validation->set_message('check_username', 'The %s <strong>"'.$username.' "</strong> is in used');	
   			return FALSE;
   		}else{
   			return TRUE;
   		}
   }
   
   public function check_email()
   {
   		$email =  $this->input->post('email');
   		$user = admin::find_by_email($email);
   		
   		if($user){
   			$this->form_validation->set_message('check_email', ' %s <strong>"'.$email.' "</strong> est utilisé.');	
   			return FALSE;
   		}else{
   			return TRUE;
   		}
   }
   
   function delete()
   {
   		$current_id = $this->uri->segment(4);
   	  	$data['user'] = admin::find_by_id($current_id);
		$data['title'] = 'Administration';
   	  	
		if(($this->input->post('id')) && ($this->admin->id != $this->input->post('id') )){
			$user = admin::find($this->input->post('id'));
   			$user->delete();
   		
   			redirect('admin990/profile');
		}elseif(($this->input->post('id')) && ($this->admin->id == $this->input->post('id') )){
			$data['error_delete'] = '<h4>you could not delete this user, because the user <span style="color:red;"> '.$this->admin->username.'</span> is in using.</h4>';
		}
		$this->load->view('admin/header',$data);
   	  	$this->load->view('admin/delete_user',$data);
   
   }
   
   private function hash_password($new_password)
  {
      $salt = bin2hex(mcrypt_create_iv(32,MCRYPT_DEV_URANDOM));
      $hash = hash('sha256',$salt.$new_password);
      
      return $salt.$hash;
  }
  
}